package com.yy.QSManage.api;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.yy.QSManage.common.Result;
import com.yy.QSManage.model.dto.FieldDto;
import com.yy.QSManage.model.entity.FaSysGrid;
import com.yy.QSManage.model.validatio.Create;
import com.yy.QSManage.model.validatio.Update;
import com.yy.QSManage.service.FaSysGridService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/FaSysGridApi")
@Api(tags = "表格")
public class FaSysGridApi extends BaseApi<FaSysGrid>{
    @Autowired
    FaSysGridService gridService;

    @ApiOperation("获取所有表")
    @GetMapping("/tables")
    public Result<List<String>> tables(){
        return Result.success(gridService.tables());
    }

    @ApiOperation("添加序号")
    @GetMapping("/addOrder")
    public Result<Boolean> addOrder(){
        List<FaSysGrid> list = gridService.list();
        for (FaSysGrid grid : list) {
            JSONArray addJson = JSONUtil.parseArray(grid.getAddJson());
            JSONArray editJson = JSONUtil.parseArray(grid.getEditJson());
            JSONArray importJson = JSONUtil.parseObj(grid.getImportJson()).getJSONArray("fields");
            JSONArray exportJson = JSONUtil.parseArray(grid.getExportJson());
            JSONArray queryJson = JSONUtil.parseArray(grid.getQueryJson());
            for (int i = 0; i < addJson.size(); i++) {
                JSONObject entries = JSONUtil.parseObj(addJson.get(i));
                entries.set("id",i +1);
                addJson.set(i,entries);
            }
            for (int i = 0; i < editJson.size(); i++) {
                JSONObject entries = JSONUtil.parseObj(editJson.get(i));
                entries.set("id",i +1);
                editJson.set(i,entries);
            }for (int i = 0; i < importJson.size(); i++) {
                JSONObject entries = JSONUtil.parseObj(importJson.get(i));
                entries.set("id",i +1);
                importJson.set(i,entries);
            }for (int i = 0; i < exportJson.size(); i++) {
                JSONObject entries = JSONUtil.parseObj(exportJson.get(i));
                entries.set("id",i +1);
                exportJson.set(i,entries);
            }for (int i = 0; i < queryJson.size(); i++) {
                JSONObject entries = JSONUtil.parseObj(queryJson.get(i));
                entries.set("id",i +1);
                queryJson.set(i,entries);
            }
            JSONObject fields = JSONUtil.parseObj(grid.getImportJson()).set("fields", importJson);
            grid.setAddJson(addJson.toString())
                    .setEditJson(editJson.toString())
                    .setImportJson(fields.toString())
                    .setExportJson(exportJson.toString())
                    .setQueryJson(queryJson.toString());
        }
        gridService.updateBatchById(list);
        return Result.success();
    }

    @Override
    public void addBefore(FaSysGrid query) {
        // 判断是否选择编辑数据源
        if(StrUtil.isBlank(query.getEditDataSourceId())){
            // 未选择编辑数据源,默认同步数据源
            query.setEditDataSourceId(query.getDataSourceId());
        }
        super.addBefore(query);
    }

    @Override
    public Result<Boolean> add(@RequestBody @Validated(Create.class) FaSysGrid query) {
        FaSysGrid initialize = gridService.initialize(query);
        return super.add(initialize);
    }

    @Override
    public void editBefore(FaSysGrid query) {
        // 判断是否选择编辑数据源
        if(StrUtil.isBlank(query.getEditDataSourceId())){
            // 未选择编辑数据源,默认同步数据源
            query.setEditDataSourceId(query.getDataSourceId());
        }
        super.editBefore(query);
    }

    @ApiOperation("编辑")
    @ApiImplicitParam(name = "query", value = "查询参数", required = true, dataType = "FaSysGrid", paramType = "body")
    @PostMapping("/editSet")
    public Result<Boolean> editSet(@RequestBody @Validated(Update.class)  FaSysGrid query){
        return Result.success(baseService.updateById(query));
    }

    @ApiOperation("初始化网格配置")
    @ApiImplicitParam(name = "id", value = "id", required = true, dataType = "String", paramType = "path")
    @GetMapping("/initialize/{id}")
    public Result<Boolean> initialize(@PathVariable String id){
        FaSysGrid faSysGrid = gridService.getById(id);
        FaSysGrid initialize = gridService.initialize(faSysGrid);
        return Result.success(baseService.updateById(initialize));
    }

    @ApiOperation("获取网格数据源字段信息")
    @ApiImplicitParam(name = "id", value = "id", required = true, dataType = "String", paramType = "path")
    @GetMapping("/fields/{id}")
    public Result<List<FieldDto>> fields(@PathVariable String id){
        return Result.success(gridService.fields(id));
    }
}
